package com.thti.mecool.client.activity;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;

import com.thti.mecool.client.adapter.TaskOptionAdapter;
import com.thti.mecool.client.base.BaseActivity;
import com.thti.mecool.client.base.Cache;
import com.thti.mecool.client.base.HandlerConstant;
import com.thti.mecool.client.base.ImageUpload;
import com.thti.mecool.client.model.biz.TaskBizImpl;
import com.thti.mecool.client.model.biz.TaskStepBizImpl;
import com.thti.mecool.client.model.po.Task;
import com.thti.mecool.client.model.po.TaskStep;
import com.thti.mecool.client.util.Util;
import com.thti.mecool.client.widget.ProgressDialogWidget;
import com.thti.mecool.client.widget.Toolbar;

public class TaskExecute extends BaseActivity {
	private Intent i;
	private ListView StepList;
	private TaskOptionAdapter TaskStepAdapter;
	private String[] StepNameArr = null,StepIds=null,parentStepIds=null;
	private String taskId = null;
	private TaskBizImpl taskBizImpl;
	private int[] ActionIds = null;
	private String PlanId, ProjectId,storeName,storeCode;
	private ProgressDialogWidget pd;
	private int uploadCount=0;
	private HashMap<String, Integer> finishStat;
	private String taskName;
	private ImageUpload imageUpload;
	private Runnable r=new Runnable() {
		@Override
		public void run() {
			if(getDataFlag(HandlerConstant.UploadImgCommand)){
				imageUpload.uploadImage(1);
				removeDataFlag(HandlerConstant.UploadImgCommand);
				System.out.println("bbbbbbbbbbbbbbbbbbbb");
				h.post(r);
			}else if(getDataFlag(HandlerConstant.UploadCurCommand)){
				System.out.println("aaaaaaaaaaaaaaaaaaaa");
				if(pd.isShowing())
					pd.dismiss("上传成功！");
				taskBizImpl.taskFinish(PlanId, taskId, ProjectId, String.valueOf(getCurrentUser().getId()));
				setResult(RESULT_OK);
				removeDataFlag(HandlerConstant.UploadCurCommand);
				finish();
			}	else{
				System.out.println("ccccccccccccccccccccc");
					h.postDelayed(r, 1000);
				}
		}
	};
	
	private Handler h;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.noTitle();
		setContentView(R.layout.mecool_task_option_list);
		Cache.add(this);
		StepList = (ListView) findViewById(R.id.mecool_task_option_list);
		i = getIntent();
		h=new Handler();
		finishStat=new HashMap<String, Integer>();
		storeName=i.getStringExtra("storeName");
		storeCode=i.getStringExtra("storeCode");
		PlanId = i.getStringExtra("planId");
		ProjectId = i.getStringExtra("projectId");
		taskName=i.getStringExtra("taskName");
		taskId=i.getStringExtra("taskId");
		TaskStepBizImpl taskStepBizImpl = new TaskStepBizImpl(this);
		if (PlanId != null & ProjectId != null) {
			List<TaskStep> taskSteps = taskStepBizImpl.findByjob(PlanId,
					ProjectId, taskId,0);
			StepNameArr = new String[taskSteps.size()];
			ActionIds = new int[taskSteps.size()];
			StepIds=new String[taskSteps.size()];
			parentStepIds=new String[taskSteps.size()];
			for (int i = 0; i < taskSteps.size(); i++) {
				parentStepIds[i]=taskSteps.get(i).getParentId();
				StepNameArr[i] = taskSteps.get(i).getName();
				ActionIds[i] = taskSteps.get(i).getActionId();
				StepIds[i]=taskSteps.get(i).getStepId();
				if(taskSteps.get(i).getActionId()!=9)
				finishStat.put(taskSteps.get(i).getStepId(), 0);
			}
		}
		if (StepNameArr != null) {
			System.out.println(Arrays.toString(StepIds));
			TaskStepAdapter = new TaskOptionAdapter(this, StepNameArr,ActionIds,finishStat,StepIds);
			StepList.setAdapter(TaskStepAdapter);
		}

		OnItemClickListener StepClickListener = new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				String parentStepId=StepIds[arg2];
				switch (ActionIds[arg2]) {
				case 1:
					i.setClass(TaskExecute.this, CameraActivity.class);
					i.putExtra("planId", PlanId);
					i.putExtra("projectId", ProjectId);
					i.putExtra("taskId", taskId);
					i.putExtra("StepId", StepIds[arg2]);
					if (taskId != null) {
						i.putExtra("parentId", StepIds[arg2]);
					}
					startActivity(i);
					break;
				// 单选框
				case 4:
					i.putExtra("parentStepId", parentStepId);
					i.setClass(TaskExecute.this, TaskStepActionActivity.class);
					i.putExtra("curStepItems", arg2);
					i.putExtra("text_name", StepNameArr[arg2]);
					i.putExtra("planId", PlanId);
					i.putExtra("projectId", ProjectId);
					i.putExtra("taskName", taskName);
					if (taskId != null) {
						i.putExtra("parentId", StepIds[arg2]);
					}
					startActivity(i);
					break;
				case 9:
					imageUpload=new ImageUpload(TaskExecute.this);
					pd=new ProgressDialogWidget(TaskExecute.this);
					Thread t=new Thread(){
						public void run() {
							imageUpload.uploadImage(0);
						};
					};
					t.start();
					Util.sendToast(TaskExecute.this, "正在上传！");
					h.post(r);
					break;
				}
			}
		};
		StepList.setOnItemClickListener(StepClickListener);
		initToolbar();
	}
@Override
protected void onResume() {
	super.onResume();
	TaskStepBizImpl taskStepBizImpl = new TaskStepBizImpl(this);
	taskBizImpl=new TaskBizImpl(this);
	List<TaskStep> taskSteps = taskStepBizImpl.findFinishSteps(ProjectId,taskId);
	if (taskSteps.size() > 0) {
		if(taskSteps.size()==finishStat.size()){
			taskBizImpl.taskFinish(PlanId, taskId, ProjectId, String.valueOf(getCurrentUser().getId()));
		}
			
		for (TaskStep ts : taskSteps) {
			finishStat.put(ts.getStepId(), 1);
		}
		TaskStepAdapter.notifyDataSetChanged();
	}

}
	public void initToolbar() {
		Toolbar toolbar = new Toolbar(this);
		toolbar.setToolbarCentreText(taskName);
		Toolbar.hideRightButton();
		Toolbar.left_btn.setOnClickListener(buttonListener);
		Toolbar.left_btn.setImageResource(R.drawable.mecool_go_back);
		Toolbar.right_btn.setVisibility(0);
		Toolbar.right_btn.setImageResource(R.drawable.mecool_add_work_memo_add);
		Toolbar.right_btn.setOnClickListener(buttonListener);
	}

	private OnClickListener buttonListener = new ImageButton.OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.mecool_toolbar_rightbtn:
				Intent in=new Intent(TaskExecute.this,AddWorkMemoActivity.class);
				in.putExtra("TaskExcuteFlag", true);
				in.putExtra("storeName", storeName);
				in.putExtra("storeCode", storeCode);
				startActivity(in);
//				in.putExtra("WorkMemoExcuteId", 10);
//				in.putExtra("workmemoshop", shop.getText().toString());
//				in.putExtra("workmemocontent", content.getText().toString());
//				in.putExtra("curItemCode", curItemCode);
//				in.putExtra("curCommId", curCommId);
				break;

			default:
				finish();
				break;
			}

		}
	};
}
